//
//  HYHomeCollectionView.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/5.
//

#import "HYHomeCollectionView.h"
#import "HYHomeFolderCollectionCell.h"
#import "HYHomeVideoCollectionCell.h"

@interface HYHomeCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HYHomeCollectionView

- (instancetype)initWithViewModel:(HYHomeViewModel *)viewModel fileType:(HYHomeFileType)fileType
{
    self = [super init];
    if(self){
        self.viewModel = viewModel;
        self.fileType = fileType;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)reloadCollect:(NSIndexPath *)indexPath
{
    if(indexPath){
        [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }else{
        [self.collectionView reloadData];
    }
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.collectionView reloadData];
    }];
    [self.viewModel.reloadSubject subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        HYHomeFileType fileType = [tuple.first integerValue];
        NSIndexPath *mainIndexPath = tuple.second;
        NSIndexPath *subIndexPath = tuple.third;
        if(self.fileType == HYHomeFileType_All){
            [self reloadCollect:mainIndexPath];
        }else if(self.fileType == fileType && (fileType == HYHomeFileType_Video || fileType == HYHomeFileType_Audio || fileType == HYHomeFileType_File || fileType == HYHomeFileType_Pic || fileType == HYHomeFileType_Other)){
            [self reloadCollect:subIndexPath];
        }else if(self.fileType == HYHomeFileType_File && fileType == HYHomeFileType_Zip){
            [self reloadCollect:subIndexPath];
        }
    }];
}

#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger item = [self.viewModel numberOfRowsIsHeader:NO fileType:self.fileType];
    collectionView.hidden = !item;
    return item;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYHomeFileType fileType = [self.viewModel fetchFileType:indexPath fileType:self.fileType];
    HYHomeBaseCollectionCell *baseCell;
    if(fileType != HYHomeFileType_Video){
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYHomeFolderCollectionCell" forIndexPath:indexPath];
    }else{
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYHomeVideoCollectionCell" forIndexPath:indexPath];
    }
    [baseCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath isHeader:NO fileType:self.fileType]];
    return baseCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectRowAtIndexPath:indexPath isHeader:NO fileType:self.fileType];
}

#pragma mark - loadLazy
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        NSInteger cloum = kIsIPad?3:2;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kItemWidth(cloum), kItemHeight(cloum));
        layout.minimumLineSpacing = kItemSpace;
        layout.minimumInteritemSpacing = kItemSpace;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#FFFFFF" darkModeHex:@"#181A22"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.contentInset = UIEdgeInsetsMake(2*kItemSpace, kItemSpace, kItemSpace, kItemSpace);
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[HYHomeFolderCollectionCell class] forCellWithReuseIdentifier:@"HYHomeFolderCollectionCell"];
        [_collectionView registerClass:[HYHomeVideoCollectionCell class] forCellWithReuseIdentifier:@"HYHomeVideoCollectionCell"];
    }
    return _collectionView;
}


@end
